package com.study.springcloud.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author xujm25077
 * @version $Id: CommonUtils.java, v 0.1 2019/4/9 14:27 域账号 xujm25077 $
 */
public class CommonUtils {

    public static byte[] ObjectToByte(Object obj) {
        byte[] bytes = null;
        try {
            // object to bytearray
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(obj);

            bytes = bo.toByteArray();

            bo.close();
            oo.close();
        } catch (Exception e) {
            System.out.println("translation" + e.getMessage());
            e.printStackTrace();
        }
        return bytes;
    }

    public static Object ByteToObject(byte[] bytes) {
        Object obj = null;
        try {
            // bytearray to object
            ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
            ObjectInputStream oi = new ObjectInputStream(bi);

            obj = oi.readObject();
            bi.close();
            oi.close();
        } catch (Exception e) {
            System.out.println("translation" + e.getMessage());
            e.printStackTrace();
        }
        return obj;
    }



    public static List readeFile(String filePath) throws Exception {
        List<String> list=new ArrayList<String>();
        InputStream input = new FileInputStream(new File(filePath));
        InputStreamReader reader = new InputStreamReader(input);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            list.add(line);
        }
        bufferedReader.close();
        return list;
    }

    public static Set<String> readFileToSet(String filePath) throws Exception {
        Set<String> list=new HashSet<>();
        InputStream input = new FileInputStream(new File(filePath));
        InputStreamReader reader = new InputStreamReader(input);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            list.add(line);
        }
        bufferedReader.close();
        return list;
    }


    public static void writeFile(Set<String> set,String filePath) throws Exception {

        File file=new File(filePath);
        if(!file.exists()){
            file.createNewFile();
        }
        OutputStream out=new FileOutputStream(filePath,true);
        OutputStreamWriter wirter=new OutputStreamWriter(out);
        BufferedWriter bufWriter=new BufferedWriter(wirter);
        for(String temp : set){
            bufWriter.write(temp.trim()+"\n");
        }
        bufWriter.close();
    }

    public static void writeFile(Map map,String filePath) throws Exception {

        File file=new File(filePath);
        if(!file.exists()){
            file.createNewFile();
        }
        OutputStream out=new FileOutputStream(filePath,true);
        OutputStreamWriter wirter=new OutputStreamWriter(out);
        BufferedWriter bufWriter=new BufferedWriter(wirter);

        for(Object entry:map.entrySet()){
            Map.Entry entry1 = (Map.Entry) entry;
            bufWriter.write(entry1.getKey()+"\t"+entry1.getValue()+"\n");
        }
        bufWriter.close();
    }


    public static void writeFile(Map<String,String> map,String filePath,String preFix) throws Exception {

        File file=new File(filePath);
        if(!file.exists()){
            file.createNewFile();
        }
        OutputStream out=new FileOutputStream(filePath,true);
        OutputStreamWriter wirter=new OutputStreamWriter(out);
        BufferedWriter bufWriter=new BufferedWriter(wirter);

        for(Map.Entry<String,String> entry:map.entrySet()){
            bufWriter.write(preFix+","+entry.getKey()+","+entry.getValue()+"\n");
        }
        bufWriter.close();
    }

    public static void writeFile2(Map<String,BigDecimal> map,String filePath,String preFix) throws Exception {

        File file=new File(filePath);
        if(!file.exists()){
            file.createNewFile();
        }
        OutputStream out=new FileOutputStream(filePath,true);
        OutputStreamWriter wirter=new OutputStreamWriter(out);
        BufferedWriter bufWriter=new BufferedWriter(wirter);

        for(Map.Entry<String,BigDecimal> entry:map.entrySet()){
            bufWriter.write(preFix+","+entry.getKey()+","+entry.getValue()+"\n");
        }
        bufWriter.close();
    }

    public static void writeFile3(Map<String,BigDecimal> map,String filePath) throws Exception {

        File file=new File(filePath);
        if(!file.exists()){
            file.createNewFile();
        }
        OutputStream out=new FileOutputStream(filePath,true);
        OutputStreamWriter wirter=new OutputStreamWriter(out);
        BufferedWriter bufWriter=new BufferedWriter(wirter);

        for(Map.Entry<String,BigDecimal> entry:map.entrySet()){
            bufWriter.write(entry.getKey()+"\n");
        }
        for(Map.Entry<String,BigDecimal> entry:map.entrySet()){
            bufWriter.write(entry.getValue()+"\n");
        }

        bufWriter.close();
    }


    public static void writeFile(List<String> set,String filePath) throws Exception {
        File file=new File(filePath);
        if(!file.exists()){
            file.createNewFile();
        }
        OutputStream out=new FileOutputStream(filePath,false);
        OutputStreamWriter wirter=new OutputStreamWriter(out);
        BufferedWriter bufWriter=new BufferedWriter(wirter);
        for(String temp : set){
            bufWriter.write(temp.trim()+"\n");
        }
        bufWriter.close();
    }


    public static void writeFileAppend(List<String> set,String filePath) throws Exception {
        File file1=new File(filePath);
        File file2=file1.getParentFile();
        if(!file2.exists()){
            file2.mkdirs();
        }
        OutputStream out=new FileOutputStream(filePath,true);
        OutputStreamWriter wirter=new OutputStreamWriter(out);
        BufferedWriter bufWriter=new BufferedWriter(wirter);
        for(String temp : set){
            bufWriter.write(temp.trim()+"\n");
        }
        bufWriter.close();
    }

}
